package com.ruoyi.web.controller.system;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SalaryStandard;
import com.ruoyi.system.service.ISalaryStandardService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工资标准Controller
 * 
 * @author mgy
 * @date 2022-11-13
 */
@Controller
@RequestMapping("/system/salaryStandard")
public class SalaryStandardController extends BaseController
{
    private String prefix = "system/salaryStandard";

    @Autowired
    private ISalaryStandardService salaryStandardService;
    @Autowired
    private ISysPostService postService;

    @RequiresPermissions("system:salaryStandard:view")
    @GetMapping()
    public String salaryStandard()
    {
        return prefix + "/salaryStandard";
    }

    /**
     * 查询工资标准列表
     */
    @RequiresPermissions("system:salaryStandard:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SalaryStandard salaryStandard)
    {
        startPage();
        List<SalaryStandard> list = salaryStandardService.selectSalaryStandardList(salaryStandard);
        return getDataTable(list);
    }

    /**
     * 导出工资标准列表
     */
    @RequiresPermissions("system:salaryStandard:export")
    @Log(title = "工资标准", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SalaryStandard salaryStandard)
    {
        List<SalaryStandard> list = salaryStandardService.selectSalaryStandardList(salaryStandard);
        ExcelUtil<SalaryStandard> util = new ExcelUtil<SalaryStandard>(SalaryStandard.class);
        return util.exportExcel(list, "工资标准数据");
    }
    @Log(title = "工资标准", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:salaryStandard:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SalaryStandard> util = new ExcelUtil<SalaryStandard>(SalaryStandard.class);
        List<SalaryStandard> list = util.importExcel(file.getInputStream());
        String message = salaryStandardService.importSalaryStandard(list, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("system:salaryStandard:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<SalaryStandard> util = new ExcelUtil<SalaryStandard>(SalaryStandard.class);
        return util.importTemplateExcel("用户数据");
    }
    /**
     * 新增工资标准
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<SysPost> postList = postService.selectPostAll();
        mmap.put("postList",postList);
        return prefix + "/add";
    }

    /**
     * 新增保存工资标准
     */
    @RequiresPermissions("system:salaryStandard:add")
    @Log(title = "工资标准", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SalaryStandard salaryStandard)
    {
        //校验岗位是否重复
        int count = salaryStandardService.count(new LambdaQueryWrapper<SalaryStandard>().eq(SalaryStandard::getPostName, salaryStandard.getPostName()));
        if (count>0)
        {
            return AjaxResult.error("新增工资标准失败，岗位已存在");
        }

        return toAjax(salaryStandardService.insertSalaryStandard(salaryStandard));
    }

    /**
     * 修改工资标准
     */
    @RequiresPermissions("system:salaryStandard:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SalaryStandard salaryStandard = salaryStandardService.selectSalaryStandardById(id);
        mmap.put("salaryStandard", salaryStandard);
        List<SysPost> postList = postService.selectPostAll();
        mmap.put("postList",postList);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资标准
     */
    @RequiresPermissions("system:salaryStandard:edit")
    @Log(title = "工资标准", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SalaryStandard salaryStandard)
    {
        //校验岗位是否重复
        int count = salaryStandardService.count(new LambdaQueryWrapper<SalaryStandard>().eq(SalaryStandard::getPostName, salaryStandard.getPostName()));
        if (count>0)
        {
            return AjaxResult.error("新增工资标准失败，岗位已存在");
        }
        return toAjax(salaryStandardService.updateSalaryStandard(salaryStandard));
    }

    /**
     * 删除工资标准
     */
    @RequiresPermissions("system:salaryStandard:remove")
    @Log(title = "工资标准", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(salaryStandardService.deleteSalaryStandardByIds(ids));
    }
}
